﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Core.Enums;
using Jayden.Dll.ECMAScript;

namespace Jayden.Console.ECMAScriptTest
{
	class Program
	{

		public delegate bool GrammarParserFunction(string script, ref int index);

		static void Test(GrammarParserFunction func, string script)
		{
			int index = 0;
			bool result = func(script, ref index);
			if (result && index != script.Length)
				System.Console.WriteLine("KO - Incomplete parse for script '{0}' for function {1}", script, func.Method.Name);
			else if(!result)
				System.Console.WriteLine("KO - Unrecognized script '{0}' with function {1}", script, func.Method.Name);
			else
				System.Console.WriteLine("OK - Parsed script '{0}' with function {1}", script, func.Method.Name);
		}

		static void Main(string[] args)
		{
			Test(GrammarParser.IsIdentifierName, "foo");
			Test(GrammarParser.IsComment, "// this is a single line comment");
			Test(GrammarParser.IsComment, "/* this is a comment */");
			Test(GrammarParser.IsLiteral, "12.5e-15");
			Test(GrammarParser.IsToken, "12.5e-15");
			Test(GrammarParser.IsToken, "'hello!\\''");
			Test(GrammarParser.IsToken, "\"hello!\\\"\"");

			Script script = new Script();
			If ifStatement = new If(Jayden.Dll.ECMAScript.Boolean.True, null);
			ifStatement.Add(Empty.Instance);
			ifStatement.Add(Debugger.Instance);
			script.Add(new ExpressionStatement(new Jayden.Dll.ECMAScript.String("use strict")));
			script.Add(new Variable("foo"));
			script.Add(ifStatement);
			script.Add(new Function("fooImpl", "cpt", "e"));

			

			StringBuilder builder = new StringBuilder();
			script.RenderStatement(0, builder);
			System.Console.WriteLine(builder.ToString());

			System.Console.WriteLine();
			System.Console.Write("=== press any key to exit ===");
			System.Console.ReadKey(true);
		}
	}
}
